# Copyright (C) 2014 chee <chee.think@gmail.com>

# This disable dependency generation if multiple architectures are set
DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)

ifndef CONFIG
	CONFIG = debug
endif

#CXX = c++-4.9

ifeq ($(CONFIG), debug)
	BINDIR := build
	LIBDIR := build
	OBJDIR := build/inter/debug
	OUTDIR := build

	TARGET := be_d

	ifeq ($(TARGET_ARCH), )
		TARGET_ARCH := -march=native
	endif

	CPPFLAGS := $(DEPFLAGS) -D "DEBUG=1" -D "_DEBUG=1"
	CFLAGS += $(CPPFLAGS) $(TARGET_ARCH) -g -ggdb -O0
	CXXFLAGS += -std=c++11 $(CFLAGS)
	LDFLAGS += $(TARGET_ARCH) -L$(BINDIR) -L$(LIBDIR)
	LDDEPS :=
	RESFLAGS := -D "DEBUG=1" -D "_DEBUG=1"

	BLDCMD = $(CXX) -o $(OUTDIR)/$(TARGET) $(OBJECTS) $(LDFLAGS) $(RESOURCES) $(TARGET_ARCH)
	CLEANCMD = rm -rf $(OUTDIR)/$(TARGET) $(OBJDIR)
endif

ifeq ($(CONFIG), release)
	BINDIR := build
	LIBDIR := build
	OBJDIR := build/inter/release
	OUTDIR := build

	TARGET := be

	ifeq ($(TARGET_ARCH), )
		TARGET_ARCH := -march=native
	endif

	CPPFLAGS := $(DEPFLAGS) -D "NDEBUG=1" 
	CFLAGS += $(CPPFLAGS) $(TARGET_ARCH) -O3
	CXXFLAGS += -std=c++11 $(CFLAGS)
	LDFLAGS += $(TARGET_ARCH) -L$(BINDIR) -L$(LIBDIR)
	LDDEPS :=
	RESFLAGS := -D "NDEBUG=1"

	BLDCMD = $(CXX) -o $(OUTDIR)/$(TARGET) $(OBJECTS) $(LDFLAGS) $(RESOURCES) $(TARGET_ARCH)
	CLEANCMD = rm -rf $(OUTDIR)/$(TARGET) $(OBJDIR)
endif

OBJECTS := \
	$(OBJDIR)/main.o

.PHONY: clean

$(OUTDIR)/$(TARGET): $(OBJECTS) $(LDDEPS) $(RESOURCES)
	@echo linking becode ...
	-@mkdir -p $(BINDIR)
	-@mkdir -p $(LIBDIR)
	-@mkdir -p $(OUTDIR)
	@$(BLDCMD)

clean:
	@echo cleaning becode ...
	@$(CLEANCMD)

strip:
	@echo stripping becode ...
	-@strip --strip-unneeded $(OUTDIR)/$(TARGET)

$(OBJDIR)/main.o: ./main.cpp
	-@mkdir -p $(OBJDIR)
	@echo "compiling main.cpp"
	@$(CXX) $(CXXFLAGS) -o "$@" -c "$<"

-include $(OBJECTS:%.o=%d)
